package org.s170.servlet.WebServlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.s170.dao.NoteDao;
import org.s170.entity.Note;
import org.s170.servlet.BaseServlet;
import org.s170.utils.PagerUtil;


@WebServlet("/api/notePage.do")
public class PageServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {

		String keyword = request.getParameter("keyword");
		PagerUtil<Note> pager = null;

		NoteDao noteDao=new NoteDao();

		// 获取当前页
				String cpStr = request.getParameter("cp");
				int cp = 1;
				if (cpStr != null) {
					cp = Integer.parseInt(cpStr);
				}

		//System.out.println("keyword=======================" + keyword);

				if (keyword == null || "".equals(keyword.trim())) {

					System.out.println("ok-----------------");
					// 第一种请况：查询全部分页
					// 把分页的信息封装在PagerUtil对象中
					pager = noteDao.selectAllByPaer(cp, 5);

				} else {
					// 第二种请况：条件查询分页
					// 对get请求中的乱码进行处理
					if ("GET".equalsIgnoreCase(request.getMethod())) {
						keyword = new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
					}


					// 把分页的信息封装在PagerUtil对象中
					pager = noteDao.selectAllByPaer(cp, 5, keyword);
				}

				print(resp,successJson(pager));

				//Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
//				System.out.println(gson);
//				System.out.println(gson.toJson(pager));
				//out.write(gson.toJson(pager));
		

	}
}
